package com.lmh.graphdisease.controller;

import com.lmh.graphdisease.entity.DiseaseEntity;
import com.lmh.graphdisease.service.DiseaseService;
import com.lmh.graphdisease.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lmh
 * @desc
 * @date 2023/3/6
 */
@RestController
@RequestMapping("/disease")
public class DiseaseController {

    @Autowired
    private DiseaseService diseaseService;


    @RequestMapping(path = "/getAllDisease",method = RequestMethod.GET)
    public ResultBean getAllDisease(){
        List<DiseaseEntity> allDisease = diseaseService.getAllDisease();
        return ResultBean.success(allDisease);
    }

    @RequestMapping(path = "/getByName", method = RequestMethod.POST)
    public ResultBean getDiseaseByName(@RequestParam(required = true) String name){
        return ResultBean.success(diseaseService.getDiseaseEntityByName(name));
    }

    /**
     *@author lmh
     *@date 2023/3/9
     *@desc 根据疾病获取疾病的相关信息词云
     */
    @RequestMapping(path = "/getCloudByName", method = RequestMethod.POST)
    public ResultBean getCloudByName(@RequestParam(required = true) String name){
        Map<String,Integer> maps = diseaseService.getCloudByName(name);
        List<String> result = new ArrayList<>();
        Set<String> keys = maps.keySet();
        for (String s:keys){
            result.add(s + "-*-" + maps.get(s) * 5);
        }
        return ResultBean.success(result);
    }

    /**
     *@author lmh
     *@date 2023/3/14
     *@desc 获取一定数量的疾病
     */
    @RequestMapping(path = "/getSomeDisease" ,method = RequestMethod.POST)
    public ResultBean getSomeDisease(@RequestParam(required = true) Integer page,@RequestParam(required = true) Integer size){
        Map<String, Object> result = diseaseService.getNumDisease(page,size);
        if(result == null || result.isEmpty()){
            return ResultBean.fail(405,"服务器错误");
        }{
            return ResultBean.success(result);
        }
    }

    /**
     *@author lmh
     *@date 2023/3/16
     *@desc 获取某个疾病的所有关系节点和关系
     */
    @RequestMapping(path = "/oneDisease",method = RequestMethod.POST)
    public ResultBean getOneDisease(@RequestParam(required = true)String name){
        Map<String,Object> result = diseaseService.getOneDisease(name);
        if(result == null || result.isEmpty()){
            return ResultBean.fail(404,"未找到该疾病，请重新搜索！");
        }else {
            return ResultBean.success(result);
        }

    }

    /**
     *@author lmh
     *@date 2023/3/22
     *@desc 辅助诊断功能实现
     */
    @RequestMapping(path = "/helpcure",method = RequestMethod.POST)
    public ResultBean getHelpCure(@RequestParam(required = true)String question) {
        String answer = diseaseService.getRobotAnswer(question);
        if(answer == null || answer == ""){
            return ResultBean.fail(404,"没查到");
        }else{
            return ResultBean.success(answer);
        }
    }


}
